<template>
	<div>
        <div style='background: #fff;padding:50px 50px 5px 50px'>
            <p class='content-head'>支付宝配置</p>
            <Form ref="form" style="width:50%" :label-width="110">
                <FormItem label="账户">
                    <p slot='label'><span class='star'>*</span>账户</p>
                    <Input v-model="form.account" placeholder="账户" ></Input>
                </FormItem>
                <FormItem label="秘钥">
                    <p slot='label'><span class='star'>*</span>秘钥</p>
                    <Input v-model="form.key" placeholder="秘钥" ></Input>
                </FormItem>
                <FormItem label="合作者身份">
                    <p slot='label'><span class='star'>*</span>合作者身份</p>
                    <Input v-model="form.partner" placeholder="合作者身份ID" ></Input>
                </FormItem>
                <FormItem>
                    <div class='btn btn-hover' @click='submit'>保存</div>
                </FormItem>
            </Form>
        </div>   
        <div style='background: #fff;padding:50px 50px 5px 50px;margin-top:16px'>
            <p class='content-head'>支付回调</p>
            <Form ref="formItem" style="width:50%" :label-width="110">
                <FormItem label='分组'>
                    <p slot='label'><span class='star'>*</span>分组</p>
                    <Select v-model='formItem.class_id' @on-change='selectFun' >
                        <Option v-for="item in configData" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem label='函数'>
                    <p slot='label'><span class='star'>*</span>函数</p>
                    <Select v-model='formItem.function_id' >
                        <Option v-for="item in funData" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <div class='btn btn-reset btn-hover' @click='payCall'>保存</div>
                </FormItem>
            </Form>
        </div>
    </div>
</template>
<script>
    export default {
        data(){
            return {
                form: {
                    account: '',
                    key: '',
                    partner:''
                },
                formItem:{
                    class_id:0,
                    function_id:0
                },
                configData:[],
                funData:[]
            }
        },
        created(){
            this.getConfig();
            //获取分组信息
            this.fetch('/home/app/auth/class').then(res=>{
                if(res&&res.data.data&&res.data.code){
                    this.configData = res.data.data;
                }
            })
        },
        methods: {
            getConfig(){
                this.fetch('/home/app/plugins/payAlipay/config').then((res) => {
                    if (res && res.data.data) {
                        let data = res.data.data;
                        this.form.account = data.account;
                        this.form.key = data.key;
                        this.form.partner = data.partner;
                        this.formItem.class_id = data.class_id;
                        this.formItem.function_id = data.function_id;
                    }
                });
            },
            //获取函数
            selectFun(classId){
                if(classId){
                    //选择函数
                    this.fetch('/home/app/engine/schedule/classId/'+classId).then(res=>{
                        if(res&&res.data.data&&res.data.code){
                            this.funData = res.data.data;
                            if(!this.funData.length){
                                this.$Message.warning('该分组下无函数')
                            }
                        }
                    })
                }
                
            },
            submit(){
            	if(!this.form.account){
                    this.$Message.warning('请填写账户')
                    return
                }
                if(!this.form.key){
                    this.$Message.warning('请填写商户密钥')
                    return
                }
                if(!this.form.partner){
                    this.$Message.warning('请填写商户合作者身份ID')
                    return
                }
                this.fetch('/home/app/plugins/payAlipay/config', this.form).then((res) => {
                    if (res && res.data.data) {
                    	this.$Message.success('保存成功')
                        this.getConfig();
                    }
                });
            },
            payCall(){
                if(!this.formItem.class_id){
                    this.$Message.warning('请选择分组')
                    return
                }
                if(!this.formItem.function_id){
                    this.$Message.warning('请选择函数')
                    return
                }
                this.fetch('/home/app/plugins/payAlipay/payCall', this.formItem).then((res) => {
                    if (res && res.data.data) {
                        this.$Message.success('保存成功')
                        this.getConfig();
                    }
                });
            }
           
        }
    }
</script>